package base.items;

import java.io.File;
import java.util.HashSet;

/**
 * The Text {@code class}. Create Text object extended from an Item.
 * 
 * @author Peter NGO, Guillaume POUSSEL and Mathieu RUL
 * 
 * @version 1.0
 */
public class Text extends Item {

	/**
	 * Author of the Text.
	 */
	private String author;

	/**
	 * Size of the Text.
	 */
	private int size;

	/**
	 * Constructor of the {@code class}. Create a Text from an ID.
	 * 
	 * @param ID
	 *            the Text ID.
	 */
	public Text(Integer ID) {
		super(ID);
		setType("Text");
	}

	/**
	 * Constructor of the {@code class}. Create a Text from parameter.
	 * 
	 * @param ID
	 *            the Text ID.
	 * 
	 * @param title
	 *            the Text title.
	 * 
	 * @param tags
	 *            the Text tags.
	 * 
	 * @param comments
	 *            the Text comments.
	 * 
	 * @param author
	 *            the Text author.
	 * 
	 * @param pages
	 *            the Text size.
	 */
	public Text(Integer ID, String title, HashSet<Integer> tags,
			HashSet<Integer> comments, String author, int pages) {
		super(ID, "Text", title, tags, comments);

		this.author = author;
		this.size = pages;
	}

	/**
	 * Returns a {@code String} array with the allowed MIME types for a Text.
	 * 
	 * @return a string array with the allowed MIME.
	 */
	@Override
	public String[] allowedMimeTypes() {
		return new String[] { "text/html", "text/plain", "text/xml" };
	}

	/**
	 * Get the author of the Text.
	 * 
	 * @return the author.
	 */
	public String getAuthor() {
		return author;
	}

	/**
	 * Get the size of the Text.
	 * 
	 * @return the size.
	 */
	public int getSize() {
		return size;
	}

	/**
	 * Parse text data for extracting the text's size.
	 * 
	 * @return {@code true} if the size was correctly extracted; {@code false}
	 *         otherwise.
	 */
	public boolean parseData() {
		if (!super.parseData())
			return false;
		File fichier = new File(getPath());
		setSize((int) fichier.length());
		return true;
	}

	/**
	 * Set the author of the Text.
	 * 
	 * @param author
	 *            the author to set.
	 */
	public void setAuthor(String author) {
		this.author = author;
	}

	/**
	 * Set the size of the Text.
	 * 
	 * @param size
	 *            the size to set.
	 */
	public void setSize(int size) {
		this.size = size;
	}

	/**
	 * Returns a {@code String} object representing the Text's value.
	 * 
	 * @return a string representation of the Text.
	 */
	@Override
	public String toString() {
		return super.toString() + "\nAuthor: " + author + "\nSize: " + size;
	}
}
